package Aptech.DAL;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;

import Aptech.DALInterface.ContractInterface;
import HibernateMappingPoJo.Builts;
import HibernateMappingPoJo.PaymentMode;
import HibernateMappingPoJo.PurchaseContract;

public class MgContract implements ContractInterface {

	  private SessionFactory sessFact;
	    private Session sess;
	    private Transaction tr;
	@Override
	public List getAllContract() {
		 List list = null;
	        try {
	            sessFact = Hibernate.HibernateUtil.getSessionFactory();
	            sess = sessFact.openSession();
	            list = sess.createCriteria(PurchaseContract.class).list();
	            sess.flush();
	        } catch (HibernateException he) {
	            System.out.println(he.getMessage());
	        } finally {
	            sess.close();
	        }

	        return list;
	}

	@Override
	public void addContract(PurchaseContract contract) {
		try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            tr = sess.beginTransaction();
            sess.save(contract);
            tr.commit();
            sess.flush();
        } catch (HibernateException he) {

            System.out.println(he.getMessage());

        } finally {
            sess.close();

        }
		
	}

	@Override
	public void deleteContract(String id) {
		try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            String sql = "delete PurchaseContract where ID = '" + id + "'";
            sess.createSQLQuery(sql).executeUpdate();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
        }
		
	}

	@Override
	public void editContract(PurchaseContract contract) {
		try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            tr = sess.beginTransaction();

            sess.update(contract);
            tr.commit();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();

        }
		
	}

	@Override
	public PurchaseContract getContractByID(String id) {
		PurchaseContract contract = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            Criteria criteria = sess.createCriteria(PurchaseContract.class).add(Restrictions.eq("id", id));
            contract = (PurchaseContract) criteria.uniqueResult();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
        }
        return contract;
	}
	
	@Override
	public List getAllPaymentMode(){
		List list = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            list = sess.createCriteria(PaymentMode.class).list();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
        }

        return list;
	}

	@Override
	public PaymentMode getPaymentModeByID(String id) {
		PaymentMode contract = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            Criteria criteria = sess.createCriteria(PaymentMode.class).add(Restrictions.eq("id", id));
            contract = (PaymentMode) criteria.uniqueResult();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
        }
        return contract;
	}
	
	public List searchForName(String key){
		
		List list = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            list = sess.createCriteria(PurchaseContract.class).add(Expression.like("contractNumber","%"+key+"%")).list();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
        }
        return list;		
	}
		
	}
	

